package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.vo.RegisterVo;
import com.ruoyi.system.mail.EmailSender;
import com.ruoyi.system.userRegister.UserRegistrationService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 新用户注册
 */
@RestController
public class UserRegistrationController {


    @Resource
    UserRegistrationService userRegistrationService;
    @Resource
    EmailSender emailSender;

    /**
     * 注册用户
     * @param registerVo
     * @return
     */
    @PostMapping("/UserRegistration")
    public AjaxResult UserRegistration(@RequestBody RegisterVo registerVo){
        String message = userRegistrationService.MailRegister(registerVo.getReceiverEmail(), registerVo.getPassword(), registerVo.getNumber());
       if (message.equals("注册成功！")){
           return AjaxResult.success(message,null);

       }else {
           return  AjaxResult.error(message,null);
       }
    }

    /**
     * 重置密码
     * @param registerVo
     * @return
     */
    @PostMapping("/UserPasswordReset")
    public AjaxResult PasswordReset(@RequestBody RegisterVo registerVo){
        String message = userRegistrationService.PasswordReset(registerVo.getReceiverEmail(), registerVo.getPassword(), registerVo.getNumber());
        if (message.equals("密码重置成功！")){
            return AjaxResult.success(message,null);

        }else {
            return  AjaxResult.error(message,null);
        }
    }

    /**
     * 发送邮件
     * @param registerVo
     */
    @PostMapping("/sendMail")
    public AjaxResult  sendMail(@RequestBody RegisterVo registerVo){
        emailSender.sendEmail(registerVo.getReceiverEmail());
        String message="请留意邮箱中的验证码！如果验证码未收到，请稍后重试或您的账号填写有误。";
        return AjaxResult.success(message,null);
    }


}
